import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation


def demo01():
    fig, ax = plt.subplots()
    x = np.arange(0, 2 * np.pi, 0.01)
    line, = ax.plot(x, np.sin(x))

    def animat(i):
        """
        第 i 帧
        """
        line.set_ydata(np.sin(x + i / 20))
        return line,

    def init():
        line.set_ydata(np.sin(x))
        return line,

    ani = animation.FuncAnimation(fig=fig, func=animat, frames=100, init_func=init, interval=1, blit=False)
    plt.show()


def demo02():
    fig, ax = plt.subplots(1, 2, figsize=(15, 3))
    p1, = ax[0].plot([], [], label='test1')
    p2, = ax[1].plot([], [], label='test2')
    ax[0].set_ylim(0,15)
    ax[1].set_ylim(0,20)
    test1 = [i for i in range(10)]
    test2 = [j for j in range(10,20)]
    ind = [i for i in range(10)]

    def anima_all(i):
        p1.set_data(ind[:i + 1], test1[:i + 1])
        p2.set_data(ind[:i + 1], test2[:i + 1])
        return p1, p2

    ani = animation.FuncAnimation(fig, anima_all, frames=10, interval=1)

    plt.legend()
    plt.show()


if __name__ == '__main__':
    demo02()
